package GUI;
/**
 * Reviewer Panel
 * @author Taylor Zielske
 * @version Decemeber 1, 2011
 */
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.Calendar;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import papers.Manuscript;
import papers.Review;

import conference.Conference;
import conference.ConferenceIO;
import conference.TextParse;
import conference.WritetoFile;

import users.Reviewer;
import users.User;
/**
 * The reviewer Panel
 * @author Taylor Zielske
 *
 */
public class ReviewerPanel extends JPanel {

	private static final long serialVersionUID = 1L;
	private Reviewer my_user;
	private Conference my_conf;
	private JPanel my_viewpanel;

	public ReviewerPanel(User user, Conference conf){
		my_viewpanel = new JPanel(new GridLayout());
		my_user = (Reviewer) user;
		my_conf = conf;
		this.setPreferredSize(new Dimension(1200,700));
		this.setLayout(new BorderLayout());
		this.setVisible(true);
		start();
		JLabel title = new JLabel("My Manuscripts to Review");
		title.setFont(new Font("Sans Serif", Font.BOLD, 20));
		title.setBorder(BorderFactory.createRaisedBevelBorder());
		this.add(title, BorderLayout.NORTH);
		this.add(my_viewpanel, BorderLayout.CENTER);
	}
	/**
	 * Creates the Review Paper Panel
	 */
	public void start(){
		for (final Manuscript m: my_user.getAssignedManuscripts()){
			boolean papersubmitted = false;
			for(int i = 0; i < my_conf.getManuscripts().size(); i++){
				if (my_conf.getManuscripts().get(i).getReviewers().contains(my_user)){
					for (Review r : my_conf.getManuscripts().get(i).getReviews()){
						if (r.getUser().equals(my_user)){
							papersubmitted = true;
						}
					}
				}
			}
			JPanel pan = new JPanel();
			pan.setLayout(new BoxLayout(pan, BoxLayout.Y_AXIS));
			final JLabel status = new JLabel();
			status.setFont(new Font("Sans Serif", Font.BOLD, 16));
			if(papersubmitted){
				status.setText("Review Submitted");
				status.setForeground(Color.GREEN.darker());
			}else{
				status.setText("Review Not Submitted");
				status.setForeground(Color.RED);
			}
			ImageIcon icon = new ImageIcon("images"
					+ File.separator + "assignedmanu.png");
			status.setAlignmentX(Component.CENTER_ALIGNMENT);
			JLabel image = new JLabel(icon, JLabel.CENTER);
			image.setAlignmentX(Component.CENTER_ALIGNMENT);
			JLabel title = new JLabel("Title: " + m.getPaperName());
			title.setAlignmentX(Component.CENTER_ALIGNMENT);
			JLabel author = new JLabel("Author: " + m.getAuthor());
			author.setAlignmentX(Component.CENTER_ALIGNMENT);
			JLabel linebreak = new JLabel(" ");
			linebreak.setAlignmentX(Component.CENTER_ALIGNMENT);
			JLabel linebreak2 = new JLabel(" ");
			linebreak2.setAlignmentX(Component.CENTER_ALIGNMENT);
			JButton addReview = new JButton("Submit Review");
			JButton download = new JButton("Download Manuscript");
			addReview.setAlignmentX(Component.CENTER_ALIGNMENT);
			download.setAlignmentX(Component.CENTER_ALIGNMENT);
			pan.add(linebreak);
			pan.add(status);
			pan.add(linebreak2);
			pan.add(author);
			pan.add(title);
			pan.add(image);
			pan.add(addReview);
			pan.add(download);
			my_viewpanel.add(pan);
			addReview.addActionListener(new ActionListener() {	
				public void actionPerformed(ActionEvent arg0) {
					JFileChooser j = new JFileChooser();
					if (j.showOpenDialog(ReviewerPanel.this) == JFileChooser.APPROVE_OPTION){
						File chosen = j.getSelectedFile();
						if (chosen.getName().endsWith(".txt")) {
							File f = new File(j.getSelectedFile().getPath());
							Review r = new Review(my_user, Calendar.getInstance().getTime(),f);
							boolean b= true;
							boolean isSubmitted = false;
							Review doup = null;
							for (Review rev : m.getReviews()){
								if(rev.getUser().equals(my_user)){
									if (JOptionPane.showConfirmDialog(
											ReviewerPanel.this,
											"A review is already submitted for this paper. " +
											"Do you want to replace this review?",
											"Replace",JOptionPane.YES_NO_OPTION)
											== JOptionPane.YES_OPTION) {
										isSubmitted = true;
										doup = rev;
										break;
									}else{
										b = false;
									}
								}
							}
							if (isSubmitted){
								m.deleteReview(doup);
							}
							if (b){
								m.addReview(r);
								status.setText("Review Submitted");
								status.setForeground(Color.GREEN.darker());
								JOptionPane.showMessageDialog(
										ReviewerPanel.this,
										"Review Submitted for " + m.getPaperName(), "Review Submitted",
										JOptionPane.INFORMATION_MESSAGE);
								ConferenceIO.saveConference(TextParse.getFileLoc(), my_conf);
							}
						}else{
							JOptionPane.showMessageDialog(
								ReviewerPanel.this,
								"Only .txt files allowed!",
								"Invalid review type",
								JOptionPane.ERROR_MESSAGE);
						}
					}
				}
			});
			download.addActionListener(new ActionListener() {	
				public void actionPerformed(ActionEvent arg0) {
					JFileChooser j = new JFileChooser();
					j.setSelectedFile(m.getPaper());
					if (j.showSaveDialog(ReviewerPanel.this) == JFileChooser.APPROVE_OPTION){
						String path = j.getSelectedFile().getAbsolutePath();
						if (!j.getSelectedFile().getAbsolutePath().contains(".txt")){
							path = j.getSelectedFile().getAbsolutePath() + ".txt";
						}
						File fileName = new File(path);
						if(fileName == null)
							return;
						try {
							String manutext = WritetoFile.getFileContent(m.getPaper());
							WritetoFile.toFileContent(manutext, fileName);
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}
			});
		}
	}
}
